/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wf3.core;

import acceso.servlet.Conpar;
import acceso.util.WorkflowUtil;
import static acceso.util.WorkflowUtil.convertir_short;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import wf3.core.user.User;
/**
 *
 * @author romulogalindo
 */
public class ManagerServlet {
    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession sesion;

    public ManagerServlet(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        sesion = this.request.getSession();
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public HttpSession getSesion() {
        return sesion;
    }
    
    public User getUser(){
        return (User) sesion.getAttribute("user");
    }
    
    //METODOS DE ACCESO DIRECTO
    
    public String get_Parameter(String name_parameter){
        return getRequest().getParameter(name_parameter);
    }
    
    public int get_Parameter_like_int(String name_parameter) throws ContenException{
        String code = get_Parameter(name_parameter);
        if(code == null) return 0;
        return WorkflowUtil.convertir_entero(code, 0);
    }
    
    public List<Conpar> get_Parameter_startwich(String name_start){
        List<Conpar> lista =  new ArrayList<>();
        Enumeration enumeration = getRequest().getParameterNames();
        while(enumeration.hasMoreElements()){
            String parametro = (String)enumeration.nextElement();
            if (parametro.startsWith(name_start)) {
                acceso.servlet.Conpar conpar = new acceso.servlet.Conpar();
                short id = convertir_short(parametro.replaceAll(name_start + "_", ""));
                String valor = "" + getRequest().getParameter(parametro);
                conpar.setId(id);
                conpar.setValor(valor);
                lista.add(conpar);
            }
        }
        return lista;
    }
    
}
